<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="串号" prop="goodsSn">
        <el-input
          v-model="queryParams.goodsSn"
          placeholder="请输入串号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入修改人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" border>
      <el-table-column label="编号" align="center" type="index" />
      <el-table-column label="商品编码" align="center" prop="goodsId" width="180" />
      <el-table-column label="类型" align="center">
        <template v-slot="scope">
          <el-tag v-for="item in typeOptions" :type="item.t" v-if="item.v === scope.row.type">{{item.l}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="goodsName" width="130" show-overflow-tooltip />
      <el-table-column label="串号" align="center" prop="goodsSn" width="160" show-overflow-tooltip />
      <el-table-column label="规格" align="center" prop="specs"  width="130" show-overflow-tooltip/>
      <el-table-column label="验机报告" align="center" prop="testInfo" width="230" show-overflow-tooltip />
      <el-table-column label="回收金额" align="center" prop="costPrice"  width="80"/>
      <el-table-column label="备注" align="center" prop="remark"  width="130" show-overflow-tooltip/>
      <el-table-column label="修改人" align="center" prop="createName" width="100" />
      <el-table-column label="修改时间" align="center" prop="createTime"  width="160"/>
      <el-table-column label="用时（秒）" align="center">
        <template v-slot="scope">
          {{getTimeSend(scope.row)}}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listRecordGoods} from "@/api/recovery/goods"
export default {
  name: "Record",
  components: {
  },
  props: {
    goodsId: {type: String}
  },
  data() {
    return {
      typeOptions: [
        {l:'修改', v: 0, t: 'info'},
        {l:'验机', v: 1, t: 'primary'},
        {l:'报价', v: 2, t: 'warning'},
        {l:'作废', v: 3, t: 'danger'},
        {l:'调价', v: 4, t: 'success'}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 修改历史记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: this.goodsId,
        goodsName: null,
        goodsSn: null,
        createName: null,
      },
    };
  },
  created() {
    this.getList();
  },
  watch:{
    goodsId(n, o) {
      this.queryParams.goodsId = n;
      this.getList();
    }
  },
  methods: {
    /** 查询修改历史记录列表 */
    getList() {
      this.loading = true;
      listRecordGoods(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTimeSend(row) {
      if(row.createTime && row.updateTime) {
        let end = new Date(row.updateTime).getTime();
        let start = new Date(row.createTime).getTime();
        return (end - start) / 1000 + "秒"
      }
      return "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
